package me.zxk.smartagriculture.dto.comment;

import lombok.Data;

import java.time.LocalDateTime;

/**
 * 评论响应DTO
 */
@Data
public class CommentResponse {

    private Long id;
    private Long newsId;
    private UserInfo user;
    private String content;
    private Integer likeCount;
    private Boolean isLiked = false; // 当前用户是否已点赞
    private LocalDateTime createTime;

    @Data
    public static class UserInfo {
        private Long id;
        private String nickname;
        private String avatarUrl;

        public UserInfo(Long id, String nickname, String avatarUrl) {
            this.id = id;
            this.nickname = nickname;
            this.avatarUrl = avatarUrl;
        }
    }

    /**
     * 从实体转换为响应DTO
     */
    public static CommentResponse fromEntity(me.zxk.smartagriculture.entity.Comment comment) {
        CommentResponse response = new CommentResponse();
        response.setId(comment.getId());
        response.setNewsId(comment.getNewsId());
        response.setContent(comment.getContent());
        response.setLikeCount(comment.getLikeCount());
        response.setCreateTime(comment.getCreateTime());
        
        // 设置用户信息
        if (comment.getUser() != null) {
            response.setUser(new UserInfo(
                comment.getUser().getId(),
                comment.getUser().getNickname(),
                comment.getUser().getAvatarUrl()
            ));
        }
        
        return response;
    }
}
